package com.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OneTaskController {

	@Autowired
	private ProcessEngine processEngine;
	
	/**
	 * 部署
	 * @return
	 */
	@GetMapping("sequence/deploytaskOne")
	public String deploySequenceFlow(){
		
		InputStream bpmn = this.getClass().getResourceAsStream("/diagrams/taskOne.bpmn");
		InputStream png = this.getClass().getResourceAsStream("/diagrams/taskOne.png");
		
		Deployment deployment = processEngine.getRepositoryService().createDeployment().name("个人任务")
			.addInputStream("taskOne.bpmn", bpmn)//与资源文件名称要一致
			.addInputStream("taskOne.png", png)
			.deploy();
		
		System.err.println(deployment.getKey() + "---" + deployment.getName());
		
		return deployment.getId();
	}
	
	/**
	 * 启动流程实例
	 * @return
	 */
	@RequestMapping("sequence/starttaskOne")
	public String startSequenceFlow(){
		
		String processDefinitionKey = "taskOne";
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", "xxx");
		
		//正在执行的流程实例和执行对象相关的service
		RuntimeService runtimeService = processEngine.getRuntimeService();
		
		//使用流程定义的key启动流程实例，key对应的是bpmn文件中的id，默认是最新版本的流程启动
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,map);
		
		System.out.println(processInstance.getId() + processInstance.getProcessDefinitionId());
		
		return processInstance.getProcessDefinitionId();
	}
	
}
